package com.zoro.barn.business.service.login.conf;

import com.zoro.barn.commons.redis.cache.RedisManagerConfig;
import com.zoro.barn.commons.webcommons.webconfig.AllWebConfig;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * @author zhaoxingwu
 */
@Import({AllWebConfig.class, RedisManagerConfig.class})
@Configuration
public class CommonConfig {

    private final RedisProperties redisProperties;

    public CommonConfig(RedisProperties redisProperties) {
        this.redisProperties = redisProperties;
    }

    @Bean
    public StringRedisTemplate stringRedisTemplate() {
        RedisStandaloneConfiguration configuration =
                new RedisStandaloneConfiguration(redisProperties.getHost(), redisProperties.getPort());
        configuration.setPassword(redisProperties.getPassword());
        configuration.setDatabase(redisProperties.getDatabase());
        LettuceConnectionFactory factory = new LettuceConnectionFactory(configuration);
        // 使用 LettuceConnectionFactory 需要手动调用一下 afterPropertiesSet，Jedis的不用，如果你把这个Factory 暴露成Bean 也不用
        factory.afterPropertiesSet();

        return new StringRedisTemplate(factory);
    }


}
